package com.kd.juhaowan.ui;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.CommonApi;
import com.kd.juhaowan.http.model.HttpListData;
import com.kd.juhaowan.http.model.PlatCoinDetal;
import com.kd.juhaowan.http.model.PlatCoinUser;
import com.kd.juhaowan.utils.ResUtils;

import java.util.List;

public class PlatFormCoinDetailActivity extends BaseActivity{

//
    private ListView moneyGridView;
    private BaseAdapter moneyAdapter;
    private ImageView backImage;
    private List<PlatCoinDetal> mMoney;
    private TextView balance,account;

    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.hnssn_activity_platform_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        setContentView(getLayoutIdentifier("hnssn_activity_platform_detail"));
//
        moneyGridView = findViewById(getViewIdentifier("platformcoin_detail_list"));
        backImage = findViewById(getViewIdentifier("platform_coin_close"));
        backImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        PlatCoinUser coin = (PlatCoinUser)getIntent().getSerializableExtra("info");
        balance = findViewById(getViewIdentifier("platform_coin_balance"));
        balance.setText("剩余："+coin.getCurrency());
        account = findViewById(getViewIdentifier("platform_coin_account"));
        account.setText("当前账号："+coin.getUsername());
//        initAdapter();
        initdata();
    }

    @Override
    protected void reInitDate() {
        initdata();
    }

    private void initdata(){
        EasyHttp.post(this).api(new CommonApi("api/ptb/CurrencyLog")).request(new HttpCallbackProxy<HttpListData<PlatCoinDetal>>(this){
            @Override
            public void onHttpSuccess(HttpListData<PlatCoinDetal> result, boolean cache) {
                super.onHttpSuccess(result, cache);
                mMoney = result.getData().getList();
            initAdapter();
//                moneyList = result.getData().getCurrencyList();
//                channelList = result.getData().getPayList();
//                myInfo = result.getData().getCurrencyUser();
//                mRate = result.getData().getProportion();
//                setData();
            }
        });
    }

//    @Override
//    public void onAsyncRequestResult(int flag, ResponseParams response, Object... params) {
//        super.onAsyncRequestResult(flag, response, params);
//        PlatFormCoinDetailResponseParams responseParams = (PlatFormCoinDetailResponseParams) response;
//        if (State.SUCCESS == responseParams.getState().getCode()) {
//            mMoney = responseParams.getData().getList();
//            initAdapter();
//        }else {
//
//        }
//    }
//
    private void initAdapter(){
        moneyAdapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return mMoney==null?0:mMoney.size();
            }

            @Override
            public Object getItem(int position) {
                return mMoney.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                MoneyViewHolder viewHolder;
                if (convertView == null) {
                    viewHolder =new MoneyViewHolder();
                    final Context context = parent.getContext();
                    LayoutInflater inflater = LayoutInflater.from(context);
                    final int layoutResId= ResUtils.getLayoutIdentifier(context, "hnssn_item_platform_detail");
                    final int dateId = ResUtils.getViewIdentifier(context, "item_platformcoin_date");
                    final int moneyResId = ResUtils.getViewIdentifier(context, "item_platformcoin_money");
                    final int typeResId = ResUtils.getViewIdentifier(context, "item_platformcoin_type");
                    convertView = inflater.inflate(layoutResId, null, false);
                    viewHolder.date = convertView.findViewById(dateId);
                    viewHolder.money = convertView.findViewById(moneyResId);
                    viewHolder.type = convertView.findViewById(typeResId);
                    convertView.setTag(viewHolder);
                }else {
                    viewHolder = (MoneyViewHolder) convertView.getTag();
                }
                PlatCoinDetal detal = mMoney.get(position);
                viewHolder.money.setText(detal.getCurrencyText());
                viewHolder.type.setText(detal.getMemo());
                String time = detal.getCreatetimeText();
                viewHolder.date.setText(time.contains(" ")?time.substring(0, time.indexOf(" ")):time);

                if(detal.getType() == 1){

                    viewHolder.type.setTextColor(Color.parseColor("#333333"));
                    viewHolder.money.setTextColor(Color.parseColor("#333333"));
                }else {
                    viewHolder.type.setTextColor(Color.parseColor("#F03047"));
                    viewHolder.money.setTextColor(Color.parseColor("#F03047"));
                }
                return convertView;
            }
        };
        moneyGridView.setAdapter(moneyAdapter);
        moneyGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                seletPosition=position;
//                moneyAdapter.notifyDataSetChanged();
            }
        });




    }

    public static class MoneyViewHolder {
        public TextView date;
        public TextView money;
        public TextView type;

    }

}
